import { createStore } from "vuex";
import { iconApi } from "../utils/api";
import { newsApi } from "../utils/api";
import { tripApi } from "../utils/api";
import { cameraApi } from "../utils/api";
import { indexImgApi } from "../utils/api";
import { indexRecommendApi } from "../utils/api";
import { indexLiveApi } from "../utils/api";
import http1 from "../utils/http1";
import { communityApi } from "../utils/api";
import { travelApi } from "../utils/api";
import { immobilizationApi } from "../utils/api";
export default createStore({
  state: {
    iconlist: [],
    newslist: [],
    triplist: [],
    cameralist: [],
    indeximg: [],
    recommendimg: [],
    code: {},
    login: {},
    loginstatus: {},
    liveimg: [],
    immobilizationList: [],
    communityList: [],
    cityName: localStorage.getItem("city")
      ? JSON.parse(localStorage.getItem("city")).name
      : "杭州",
    cityId: localStorage.getItem("city")
      ? JSON.parse(localStorage.getItem("city")).id
      : "330100",
    cityList: [],
    hotCity: [],
    travellist: {},
  },
  mutations: {
    seticon(state, payload) {
      state.iconlist = payload.result;
    },
    changeCity(state, payload) {
      state.cityName = payload.name;
      state.cityId = payload.id;
      localStorage.setItem("city", JSON.stringify(payload));
    },
    changeCityList(state, payload) {
      state.cityList = payload.result.slice(1);
    },
    changehotCity(state, payload) {
      state.hotCity = payload.result.slice(0, 1)[0].cities;
      console.log(state.hotCity);
    },
    setnews(state, payload) {
      state.newslist = payload.result;
    },
    settravel(state, payload) {
      state.travellist = payload.list;
    },
    indeximg(state, payload) {
      state.indeximg = payload.result;
      // console.log(state.indeximg);
    },
    recommendimg(state, payload) {
      state.recommendimg = payload.result;
      console.log(state.recommendimg);
    },
    setCode(state, payload) {
      state.code = payload;
    },
    setLogin(state, payload) {
      state.login = payload;
      state.loginstatus = payload.status;
      console.log(state.login);
    },
    liveImg(state, payload) {
      state.liveimg = payload.result;
    },
    //社区_固定背景图
    setimmobilization(state, payload) {
      state.immobilizationList = payload.result;
    },
    //社区_评论
    setcommunity(state, payload) {
      state.communityList = payload.result;
    },
    settrip(state, payload) {
      state.triplist = payload.result;
    },
    setcamera(state, payload) {
      state.cameralist = payload.result;
    },
  },

  actions: {
    async geticon({ commit }) {
      const res = await iconApi();
      commit("seticon", res);
    },

    async gettrip({ commit }) {
      const res2 = await tripApi();
      commit("settrip", res2);
    },
    async getcamera({ commit }) {
      const res3 = await cameraApi();
      commit("setcamera", res3);
    },
    async gettravel({ commit }) {
      const res2 = await travelApi();
      commit("settravel", res2);
    },
    async getnews({ commit }) {
      const res1 = await newsApi();
      commit("setnews", res1);
    },
    async getIndexImg({ commit }) {
      const res = await indexImgApi();
      // console.log(res);
      commit("indeximg", res);
    },
    async getRecommendImg({ commit }) {
      const res = await indexRecommendApi();
      console.log(res);
      commit("recommendimg", res);
    },
    async getCode({ commit }, payload) {
      console.log(payload);
      const res = await http1.post(
        "http://www.pudge.wang:4000/register/getCode",
        { phone: payload.phone, templateId: payload.templateId }
      );
      console.log(res);
      commit("setCode", res);
    },
    getLogin({ commit }, payload) {
      return new Promise((resolve) => {
        http1
          .post("http://www.pudge.wang:4000/register", {
            phone: payload.phone,
            code: payload.code,
          })
          .then((res) => {
            console.log(123);
            commit("setLogin", res);
            console.log(res);
            resolve(res);
          });
      });
    },
    async getLiveImg({ commit }) {
      const res = await indexLiveApi();
      console.log(res);
      commit("liveImg", res);
    },

    //请求社区页面_固定背景图
    async getimmobilization({ commit }) {
      const res = await immobilizationApi();
      commit("setimmobilization", res);
    },
    //请求社区页面_评论
    async getcommunity({ commit }) {
      const res = await communityApi();
      commit("setcommunity", res);
    },
    async getCityList({ commit }) {
      // fetch("http://www.pudge.wang:3080/api/area/list")
      //     .then((response) => response.json())
      //     .then((res) => {
      //         // this.cityList = res.result.slice(1);
      //         commit("changeCityList", res);
      //         commit("changehotCity", res);

      //         // this.hotCity = res.result.slice(0, 1)[0].cities;
      //     });
      // http.get("http://www.pudge.wang:3080/api/area/list").then((res) => {
      //     commit("changeCityList", res);
      //     commit("changehotCity", res);
      // });
      const res = await http1.get("http://www.pudge.wang:3080/api/area/list");
      commit("changeCityList", res);
      commit("changehotCity", res);
    },
  },
});
